#ifdef KINECT

#include <QDesktopWidget>

#include "inc/GUI/kinect/kinectdialog.h"

#ifdef WIIMOTE
#include "GUI/wiimote/wiimotestatusdialog.h"
#endif

#include "GUI/kinect/kinectcalibrationdialog.h"

KinectDialog::KinectDialog(kinect::Kinect* kinect, QWidget *parent) :
    QDialog(parent),
    _currentState(NULL),
    _kinect(kinect),
    _skeleton(_kinect->addComponent<kinect::KinectSkeleton>())
{
    connect(_skeleton, SIGNAL(skeletonChanged(QSharedPointer<UserSkeleton>)), this, SLOT(processSkeleton(QSharedPointer<UserSkeleton>)));

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
}

KinectDialog::~KinectDialog() {
}

bool KinectDialog::calibrate(kinect::Kinect* kinect, QWidget* parent) {
    kinect::KinectSkeleton* skeleton = kinect->addComponent<kinect::KinectSkeleton>();
    if (skeleton->trackingState() != kinect::KinectSkeleton::Tracking) {
        KinectCalibrationDialog* dialog = new KinectCalibrationDialog(kinect, parent);
        if (dialog->exec() == QDialog::Accepted) {
            dialog->deleteLater();
            // For some reason, these weren't being deleted, and the signals/slots connected to them kept firing.
            return true;
        } else {
            // Cancel the window if they cancel calibration.
            dialog->deleteLater();
            return false;
        }
    } else {
        return true;
    }
}

#ifdef WIIMOTE
bool KinectDialog::calibrate(kinect::Kinect *kinect, WiimoteStatusDialog *wiimoteDialog, QWidget *parent) {
    kinect::KinectSkeleton* skeleton = kinect->addComponent<kinect::KinectSkeleton>();
    if (skeleton->trackingState() != kinect::KinectSkeleton::Tracking) {
        KinectCalibrationDialog* dialog = new KinectCalibrationDialog(kinect, parent);
        QWidget* old;
        if (wiimoteDialog != NULL) {
            old = wiimoteDialog->associate(dialog);
        }
        if (dialog->exec() == QDialog::Accepted) {
            if (wiimoteDialog != NULL) {
                wiimoteDialog->associate(old);
            }
            dialog->deleteLater();
            // For some reason, these weren't being deleted, and the signals/slots connected to them kept firing.
            return true;
        } else {
            // Cancel the window if they cancel calibration.
            if (wiimoteDialog != NULL) {
                wiimoteDialog->associate(old);
            }
            dialog->deleteLater();
            return false;
        }
    } else {
        return true;
    }
}
#endif

void KinectDialog::calibrateKinect() {
#ifdef WIIMOTE
    if (!KinectDialog::calibrate(_kinect, getWiimoteDialog(), this)) {
#else
    if (!KinectDialog::calibrate(_kinect, this)) {
#endif
        reject();
    }
}

kinect::Kinect* KinectDialog::getKinect() {
    return _kinect;
}

QSharedPointer<TroyPose> KinectDialog::getTroyPose() {
    return _currentState;
}

kinect::KinectSkeleton* KinectDialog::getKinectSkeleton() {
    return _skeleton;
}

void KinectDialog::maximizeSize() {
    // Resize the window to fill the whole screen.
    QApplication* app = static_cast<QApplication*>(QApplication::instance());
    resize(app->desktop()->availableGeometry().width() - frameSize().width() + geometry().width(), app->desktop()->availableGeometry().height() - frameSize().height() + geometry().height());
    move(0, 0);
}

void KinectDialog::processSkeleton(QSharedPointer<UserSkeleton> skeleton) {
    //qDebug() << "KinectDialog receiving.";
    QMap<int, float> hints;
    if (!_currentState.isNull()) {
        foreach (int joint, _currentState->joints()) {
            hints.insert(joint, _currentState->jointAngle(joint));
        }
    }
    QSharedPointer<TroyPose> newPose(new TroyPose(skeleton, hints));
    _currentState.swap(newPose);
    receiveTroyPose(_currentState);
}

#endif
